﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OntologyCache;
using biolit_config;
using System.Xml.XPath;
using System.IO;
using System.Collections;

namespace UCSD_BioLit
{
    public partial class DownloadHistory : Form
    {
        public DownloadHistory()
        {
            InitializeComponent();
        }
        string URL = string.Empty;
        XPathNavigator _navigator;
        XPathDocument _docNavigator;
        XPathNodeIterator _nodeIterator;
        OntList ontologyList = null;
        string presentVersion = string.Empty;
        private void DownloadHistory_Load(object sender, EventArgs e)
        {
            ConfigSettings cs = Biolit_Settings.Settings;
            dataGridView1.Columns.Add("OntologyName", "Ontology Name");
            dataGridView1.Columns.Add("Format", "Format");
            dataGridView1.Columns.Add("Status", "Status");
            dataGridView1.Columns.Add("DateTime", "Time Stamp");
            dataGridView1.Columns.Add("Size", "File Size");
            dataGridView1.Columns.Add("Source", "Source File");
            dataGridView1.Columns.Add("Version", "Downloaded Version");
            dataGridView1.Columns.Add("CurrentVersion", "Present Version");
            List<DownloadCompare> dCompare = new List<DownloadCompare>();
            if (Globals.ThisAddIn.m_SelectedOntologies != null && Globals.ThisAddIn.m_SelectedOntologies.Count > 0 && Biolit_Settings.Settings.OBO_DWNLDS != null)
            {
                foreach (OntList oList in Globals.ThisAddIn.m_SelectedOntologies)
                {
                    if (!String.IsNullOrEmpty(oList.Name) && !String.IsNullOrEmpty(oList.Format))
                    {
                        if (File.Exists(Path.Combine(Biolit_Settings.Settings.OBO_DWNLDS, oList.Name + "." + oList.Format)))
                        {
                            string Size = string.Empty;
                            string FileDateTime = string.Empty;
                            URL = "http://rest.bioontology.org/bioportal/ontologies/" + oList.ID.ToString() + @"?apikey=bd750df3-3ec6-4383-b506-6ad887cfdcd1";
                            _docNavigator = new XPathDocument(URL);
                            _navigator = _docNavigator.CreateNavigator();
                            _nodeIterator = _navigator.Select("/success/data/ontologyBean");
                            while (_nodeIterator.MoveNext())
                            {
                                XPathNavigator versionNumber = _nodeIterator.Current.SelectSingleNode("versionNumber");

                                if (versionNumber != null)
                                {
                                    ontologyList = new OntList(_nodeIterator.Current.SelectSingleNode("displayLabel").Value, _nodeIterator.Current.SelectSingleNode("id").Value, _nodeIterator.Current.SelectSingleNode("versionNumber").Value, _nodeIterator.Current.SelectSingleNode("format").Value, "Rest WebServices");
                                }
                            };
                            if (ontologyList != null)
                                presentVersion = ontologyList.Version;
                            FileInfo fi = new FileInfo(Path.Combine(Biolit_Settings.Settings.OBO_DWNLDS, oList.Name + "." + oList.Format));
                            if (fi.Length > 0)
                            {
                                Size = ToByteString(fi.Length);
                                FileDateTime = fi.LastWriteTime.ToString();
                            }
                            else
                                Size = "0 Bytes";
                            if (!String.IsNullOrEmpty(oList.ID))
                                dCompare.Add(new DownloadCompare(oList.Name, oList.Format.ToUpper(), "Success", Convert.ToDateTime(FileDateTime), Size, "Rest WebServices", oList.Version, presentVersion));
                            else
                                dCompare.Add(new DownloadCompare(oList.Name, oList.Format.ToUpper(), "Success", Convert.ToDateTime(FileDateTime), Size, "Local File", string.Empty, string.Empty));
                        }
                    }
                }
            }
            if (dCompare.Count > 0)
            {
                int rowNumber = 0;
                if (cs.downloadsSettings.Equals(0))
                    rowNumber = 10;
                else
                    rowNumber = cs.downloadsSettings;
                dCompare.Sort();
                foreach (DownloadCompare dnCompare in dCompare)
                {
                    if (dataGridView1.Rows.Count > rowNumber) break;
                    dataGridView1.Rows.Add(dnCompare.OntologyName, dnCompare.OntologyFormat, dnCompare.Status, Convert.ToString(dnCompare.FileDate),dnCompare.FileSize, dnCompare.Source, dnCompare.DownloadVer, dnCompare.PresentVer );
                }
            }
        }

        public static string ToByteString(long bytes)
        {
            long KB = 1024;
            long MB = 1024 * KB;
            long GB = 1024 * MB;
            long TB = 1024 * GB;
            if (bytes > TB) return (bytes / TB).ToString("0.00 TB");
            else if (bytes > GB) return (bytes / GB).ToString("0.00 GB");
            else if (bytes > MB) return (bytes / MB).ToString("0.00 MB");
            else if (bytes > KB) return (bytes / KB).ToString("0.00 KB");
            else return bytes + " Bytes";
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    public class DownloadCompare : IComparable
    {
        public string OntologyName { get; set; }
        public string OntologyFormat { get; set; }
        public string Status { get; set; }
        public DateTime FileDate { get; set; }
        public string FileSize { get; set; }
        public string Source { get; set; }
        public string DownloadVer { get; set; }
        public string PresentVer { get; set; }

        public DownloadCompare(string OntologyName, string OntologyFormat, string Status, DateTime FileDate, string FileSize, string Source, string DownloadVer, string PresentVer)
        {
            this.OntologyName = OntologyName;
            this.OntologyFormat = OntologyFormat;
            this.Status = Status;
            this.FileDate = FileDate;
            this.FileSize = FileSize;
            this.Source = Source;
            this.DownloadVer = DownloadVer;
            this.PresentVer = PresentVer;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            DownloadCompare dwnldCompare = (DownloadCompare)obj;
            if (this.FileDate.CompareTo(dwnldCompare.FileDate) > 0)
                return -1;
            if (this.FileDate.CompareTo(dwnldCompare.FileDate) < 0)
                return 1;
            if (this.FileDate.CompareTo(dwnldCompare.FileDate) == 0)
                return 0;
            return 0;
        }

        #endregion
    }

}
